﻿namespace Dcms.DAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public class Dcms_PositionDAL : IDcms_PositionDAL
    {
        public Dcms_PositionModel AddModel(Dcms_PositionModel model)
        {
            string sQLString = "insert into Dcms_Position(Position_CateId,Position_Name,Position_Num,Position_Area,Position_AddTime,Position_ValidTime,Position_Description,Position_Conditions,Position_Contact,Position_Count,Position_Departments,Position_State,Position_Edu,Position_ExFlag1,Position_ExFlag2,Position_ExFlag3,Position_ExFlag4,Position_ExFlag5,Position_ExFlag6,Position_ExFlag7,Position_ExFlag8,Position_ExFlag9,Position_ExFlag10) values(@Position_CateId,@Position_Name,@Position_Num,@Position_Area,@Position_AddTime,@Position_ValidTime,@Position_Description,@Position_Conditions,@Position_Contact,@Position_Count,@Position_Departments,@Position_State,@Position_Edu,@Position_ExFlag1,@Position_ExFlag2,@Position_ExFlag3,@Position_ExFlag4,@Position_ExFlag5,@Position_ExFlag6,@Position_ExFlag7,@Position_ExFlag8,@Position_ExFlag9,@Position_ExFlag10) SELECT @NewId=@@Identity";
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@NewId", SqlDbType.Int), new SqlParameter("@Position_CateId", SqlDbType.Int), new SqlParameter("@Position_Name", SqlDbType.VarChar), new SqlParameter("@Position_Num", SqlDbType.Int), new SqlParameter("@Position_Area", SqlDbType.VarChar), new SqlParameter("@Position_AddTime", SqlDbType.DateTime), new SqlParameter("@Position_ValidTime", SqlDbType.DateTime), new SqlParameter("@Position_Description", SqlDbType.Text), new SqlParameter("@Position_Conditions", SqlDbType.Text), new SqlParameter("@Position_Contact", SqlDbType.Text), new SqlParameter("@Position_Count", SqlDbType.Int), new SqlParameter("@Position_Departments", SqlDbType.VarChar), new SqlParameter("@Position_State", SqlDbType.VarChar), new SqlParameter("@Position_Edu", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag1", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag2", SqlDbType.VarChar), 
                new SqlParameter("@Position_ExFlag3", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag4", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag5", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag6", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag7", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag8", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag9", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag10", SqlDbType.VarChar)
             };
            cmdParms[0].Direction = ParameterDirection.Output;
            cmdParms[1].Value = model.Position_CateId;
            cmdParms[2].Value = model.Position_Name;
            cmdParms[3].Value = model.Position_Num;
            cmdParms[4].Value = model.Position_Area;
            cmdParms[5].Value = model.Position_AddTime;
            cmdParms[6].Value = model.Position_ValidTime;
            cmdParms[7].Value = model.Position_Description;
            cmdParms[8].Value = model.Position_Conditions;
            cmdParms[9].Value = model.Position_Contact;
            cmdParms[10].Value = model.Position_Count;
            cmdParms[11].Value = model.Position_Departments;
            cmdParms[12].Value = model.Position_State;
            cmdParms[13].Value = model.Position_Edu;
            cmdParms[14].Value = model.Position_ExFlag1;
            cmdParms[15].Value = model.Position_ExFlag2;
            cmdParms[0x10].Value = model.Position_ExFlag3;
            cmdParms[0x11].Value = model.Position_ExFlag4;
            cmdParms[0x12].Value = model.Position_ExFlag5;
            cmdParms[0x13].Value = model.Position_ExFlag6;
            cmdParms[20].Value = model.Position_ExFlag7;
            cmdParms[0x15].Value = model.Position_ExFlag8;
            cmdParms[0x16].Value = model.Position_ExFlag9;
            cmdParms[0x17].Value = model.Position_ExFlag10;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            model.Position_Id = (int) cmdParms[0].Value;
            return model;
        }

        public IList<Dcms_PositionModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_PositionModel> list = new List<Dcms_PositionModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_PositionModel item = new Dcms_PositionModel();
                if (ds.Tables[0].Rows[i]["Position_Id"].ToString() != "") item.Position_Id = int.Parse(ds.Tables[0].Rows[i]["Position_Id"].ToString());
                if (ds.Tables[0].Rows[i]["Position_CateId"].ToString() != "") item.Position_CateId = int.Parse(ds.Tables[0].Rows[i]["Position_CateId"].ToString());
                item.Position_Name = ds.Tables[0].Rows[i]["Position_Name"].ToString();
                if (ds.Tables[0].Rows[i]["Position_Num"].ToString() != "") item.Position_Num = int.Parse(ds.Tables[0].Rows[i]["Position_Num"].ToString());
                item.Position_Area = ds.Tables[0].Rows[i]["Position_Area"].ToString();
                if (ds.Tables[0].Rows[i]["Position_AddTime"].ToString() != "") item.Position_AddTime = DateTime.Parse(ds.Tables[0].Rows[i]["Position_AddTime"].ToString());
                if (ds.Tables[0].Rows[i]["Position_ValidTime"].ToString() != "") item.Position_ValidTime = DateTime.Parse(ds.Tables[0].Rows[i]["Position_ValidTime"].ToString());
                item.Position_Description = ds.Tables[0].Rows[i]["Position_Description"].ToString();
                item.Position_Conditions = ds.Tables[0].Rows[i]["Position_Conditions"].ToString();
                item.Position_Contact = ds.Tables[0].Rows[i]["Position_Contact"].ToString();
                if (ds.Tables[0].Rows[i]["Position_Count"].ToString() != "") item.Position_Count = int.Parse(ds.Tables[0].Rows[i]["Position_Count"].ToString());
                item.Position_Departments = ds.Tables[0].Rows[i]["Position_Departments"].ToString();
                item.Position_State = ds.Tables[0].Rows[i]["Position_State"].ToString();
                item.Position_Edu = ds.Tables[0].Rows[i]["Position_Edu"].ToString();
                item.Position_ExFlag1 = ds.Tables[0].Rows[i]["Position_ExFlag1"].ToString();
                item.Position_ExFlag2 = ds.Tables[0].Rows[i]["Position_ExFlag2"].ToString();
                item.Position_ExFlag3 = ds.Tables[0].Rows[i]["Position_ExFlag3"].ToString();
                item.Position_ExFlag4 = ds.Tables[0].Rows[i]["Position_ExFlag4"].ToString();
                item.Position_ExFlag5 = ds.Tables[0].Rows[i]["Position_ExFlag5"].ToString();
                item.Position_ExFlag6 = ds.Tables[0].Rows[i]["Position_ExFlag6"].ToString();
                item.Position_ExFlag7 = ds.Tables[0].Rows[i]["Position_ExFlag7"].ToString();
                item.Position_ExFlag8 = ds.Tables[0].Rows[i]["Position_ExFlag8"].ToString();
                item.Position_ExFlag9 = ds.Tables[0].Rows[i]["Position_ExFlag9"].ToString();
                item.Position_ExFlag10 = ds.Tables[0].Rows[i]["Position_ExFlag10"].ToString();
                list.Add(item);
            }
            return list;
        }

        public bool DeleteAllCheck(string AllID)
        {
            return SqlHelper.ExecuteSql("delete from Dcms_Position where Position_Id In (" + AllID + ")", null) > 0;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_Position where Position_Id=@Position_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Position_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public string GetDataCount(string SqlStr)
        {
            return SqlHelper.GetNoteCount(SqlStr).ToString();
        }

        public IList<Dcms_PositionModel> GetDataListByPage(string SqlStr, int CurPage, int PageSize)
        {
            return this.DataSetToList(SqlHelper.CreateDataSet(SqlStr, (CurPage - 1) * PageSize, PageSize));
        }

        public Dcms_PositionModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_Position where Position_Id=@Position_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Position_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            Dcms_PositionModel model = new Dcms_PositionModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_PositionModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataSet GetModelList()
        {
            string sql = "select * from Dcms_Position";
            return SqlHelper.CreateDataSet(sql);
        }

        public Dcms_PositionModel GetNewModelByCatID(int CatID)
        {
            return null;
        }

        public Dcms_PositionModel Update(Dcms_PositionModel model)
        {
            string sQLString = "update Dcms_Position set Position_CateId=@Position_CateId,Position_Name=@Position_Name,Position_Num=@Position_Num,Position_Area=@Position_Area,Position_AddTime=@Position_AddTime,Position_ValidTime=@Position_ValidTime,Position_Description=@Position_Description,Position_Conditions=@Position_Conditions,Position_Contact=@Position_Contact,Position_Count=@Position_Count,Position_Departments=@Position_Departments,Position_State=@Position_State,Position_Edu=@Position_Edu,Position_ExFlag1=@Position_ExFlag1,Position_ExFlag2=@Position_ExFlag2,Position_ExFlag3=@Position_ExFlag3,Position_ExFlag4=@Position_ExFlag4,Position_ExFlag5=@Position_ExFlag5,Position_ExFlag6=@Position_ExFlag6,Position_ExFlag7=@Position_ExFlag7,Position_ExFlag8=@Position_ExFlag8,Position_ExFlag9=@Position_ExFlag9,Position_ExFlag10=@Position_ExFlag10 where Position_Id=@Position_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@Position_CateId", SqlDbType.Int), new SqlParameter("@Position_Name", SqlDbType.VarChar), new SqlParameter("@Position_Num", SqlDbType.Int), new SqlParameter("@Position_Area", SqlDbType.VarChar), new SqlParameter("@Position_AddTime", SqlDbType.DateTime), new SqlParameter("@Position_ValidTime", SqlDbType.DateTime), new SqlParameter("@Position_Description", SqlDbType.Text), new SqlParameter("@Position_Conditions", SqlDbType.Text), new SqlParameter("@Position_Contact", SqlDbType.Text), new SqlParameter("@Position_Count", SqlDbType.Int), new SqlParameter("@Position_Departments", SqlDbType.VarChar), new SqlParameter("@Position_State", SqlDbType.VarChar), new SqlParameter("@Position_Edu", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag1", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag2", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag3", SqlDbType.VarChar), 
                new SqlParameter("@Position_ExFlag4", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag5", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag6", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag7", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag8", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag9", SqlDbType.VarChar), new SqlParameter("@Position_ExFlag10", SqlDbType.VarChar), new SqlParameter("@Position_Id", SqlDbType.Int)
             };
            cmdParms[0].Value = model.Position_CateId;
            cmdParms[1].Value = model.Position_Name;
            cmdParms[2].Value = model.Position_Num;
            cmdParms[3].Value = model.Position_Area;
            cmdParms[4].Value = model.Position_AddTime;
            cmdParms[5].Value = model.Position_ValidTime;
            cmdParms[6].Value = model.Position_Description;
            cmdParms[7].Value = model.Position_Conditions;
            cmdParms[8].Value = model.Position_Contact;
            cmdParms[9].Value = model.Position_Count;
            cmdParms[10].Value = model.Position_Departments;
            cmdParms[11].Value = model.Position_State;
            cmdParms[12].Value = model.Position_Edu;
            cmdParms[13].Value = model.Position_ExFlag1;
            cmdParms[14].Value = model.Position_ExFlag2;
            cmdParms[15].Value = model.Position_ExFlag3;
            cmdParms[0x10].Value = model.Position_ExFlag4;
            cmdParms[0x11].Value = model.Position_ExFlag5;
            cmdParms[0x12].Value = model.Position_ExFlag6;
            cmdParms[0x13].Value = model.Position_ExFlag7;
            cmdParms[20].Value = model.Position_ExFlag8;
            cmdParms[0x15].Value = model.Position_ExFlag9;
            cmdParms[0x16].Value = model.Position_ExFlag10;
            cmdParms[0x17].Value = model.Position_Id;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public void UpDateCount(int ID)
        {
            string sQLString = "Update Dcms_Position Set Position_Count=(Position_Count+1) where Position_Count=@Position_Count";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Position_Count", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public bool UpdateState(string AllID, int StateValue)
        {
            return SqlHelper.ExecuteSql("Update Dcms_Position Set Position_State='" + StateValue.ToString() + "' where Position_Id In (" + AllID + ")", null) > 0;
        }
    }
}

